# encoding: utf-8
""" 
@version: v1.0 
@author: autumner 
@license: Apache Licence  
@contact: 18322313385@163.com 
@site:  
@software: PyCharm 
@file: class_proxy.py 
@time: 2019/6/25 10:54
"""
'''
类属性的代理访问
1.类方法多的时候可以使用__getattr__()进行代理方法
'''

class A:
    def spam(self, x):
        return x

    def foo(self):
        pass

class B:
    def __init__(self):
        self._a = A()

    def bar(self, x):
        return x

    # Expose all of the methods defined on class A
    def __getattr__(self, item):
        '''这个方法在访问attribute不存在的时候被调用'''
        return getattr(self._a, item)

# Example use
b = B()
print(b.bar(3))
print(b.spam(32))